#	Copyright (C) 2011 Salil Bhagurkar
#
#	This file is part of illusion
#
#	illusion is free software: you can redistribute it and/or modify
#	it under the terms of the GNU Lesser General Public License as
#	published by the Free Software Foundation, either version 3 of
#	the License, or (at your option) any later version.
#
#	illusion is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU Lesser General Public License for more details.
#
#	You should have received a copy of the GNU Lesser General Public
#	License along with illusion.
#	If not, see <http://www.gnu.org/licenses/>.

CC := i686-elf-gcc
CFLAGS := -O0 -I. -c -ffreestanding -nostdinc -g -std=c99 -fleading-underscore -Wno-unused
LD := i686-elf-ld
LDFLAGS := -T./link.ld -nostdlib -nodefaultlibs
LDFLAGS_MAP := -T./link.ld -nostdlib -nodefaultlibs -Map ldmap.txt
AS := nasm
ASFLAGS := -felf32

BUILD_NUMBER_FILE := buildcount

BUILD_NUMBER := $$(cat $(BUILD_NUMBER_FILE))

BUILD_NUMBER_LDFLAGS  = --defsym __BUILD_DATE=$$(date +'%Y%m%d')
BUILD_NUMBER_LDFLAGS += --defsym __BUILD_NUMBER=$$(cat $(BUILD_NUMBER_FILE))

OUTPUT := illusion.exe
ISO := illusion.iso
ISOFILES := isofiles


####################RAMDISK Configuration#############################
RDISK_OUTPUT := rdisk.img
######################################################################

EMUL := "qemu"
EMUL_ARGS := -cdrom $(ISO) -serial file:serlog.txt -boot d


OBJECTS_ARCH := x86/boot.o x86/init.o x86/console.o x86/page.o x86/multiboot.o
OBJECTS_KERN := kernel/init.o kernel/console.o kernel/drivers.o kernel/kmalloc.o kernel/page.o kernel/syscall.o kernel/module.o kernel/vmpage.o kernel/process.o kernel/vfs.o kernel/lfault.o kernel/sched.o kernel/irq.o kernel/input.o kernel/errors.o kernel/storage.o kernel/fs.o kernel/power.o kernel/timing.o kernel/sys.o kernel/exception.o kernel/event.o kernel/glist.o
OBJECTS_DEV := dev/init.o dev/biosdisk.o dev/pckbd.o dev/fatfs.o
OBJECTS_TESTS := tests/tests.o
OBJECTS_LIB := klib/memory.o klib/format.o klib/string.o klib/bitmap.o
OBJECTS := $(OBJECTS_ARCH) $(OBJECTS_LIB) $(OBJECTS_KERN) $(OBJECTS_TESTS) $(OBJECTS_DEV)

MISC_CRAP := parport.out bochsout.txt

all: build_number kernel iso

build_number:
	@if ! test -f $(BUILD_NUMBER_FILE); then echo 0 > $(BUILD_NUMBER_FILE); fi
	@echo $$(($$(cat $(BUILD_NUMBER_FILE)) + 1)) > $(BUILD_NUMBER_FILE)

kernel: prepare_include $(OBJECTS) $(BUILD_NUMBER_FILE)
	$(LD) $(LDFLAGS_MAP) $(BUILD_NUMBER_LDFLAGS) -o $(OUTPUT) $(OBJECTS)

iso: rdisk
	cp $(OUTPUT) $(ISOFILES)/boot/$(OUTPUT)
	cp rdisk.img $(ISOFILES)/boot/rdisk.img
	genisoimage -input-charset ascii -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -o $(ISO) $(ISOFILES)

prepare_include: x86/*.h
	mkdir -p arch
	cp x86/*.h arch/




#Illusion Real Mode Manager for x86
irmm.h: irmm.bin
	tools/bin2c.exe x86/irmm.h x86/irmm.bin
	
irmm.bin:
	nasm -fbin -o x86/irmm.bin x86/irmm.asm






#RAMDISK Contents
rdisk: apimod_the ishell testapp ls cat catsys lsdev umtest create write
	tools/mkrdisk.exe $(RDISK_OUTPUT) COPYING COPYING.LESSER apps/ishell/ishell.exe apps/ishell/ishell_config.ini apps/ishell/ishell_help.txt apps/testapp/testapp.exe apps/ls/ls.exe apps/cat/cat.exe apps/catsys/catsys.exe apps/catsys/catsys.ini apps/lsdev/lsdev.exe apps/umtest/umtest.exe apps/create/create.exe apps/write/write.exe apimod/apimod.exe resrc/ident.txt

apimod_the:
	make -C apimod

ishell:
	make -C apps OBJECTS="ishell/ishell.o ishell/parse.o" OUTPUT="ishell/ishell.exe"

testapp:
	make -C apps OBJECTS=testapp/testapp.o OUTPUT=testapp/testapp.exe

ls:
	make -C apps OBJECTS=ls/ls.o OUTPUT=ls/ls.exe

cat:
	make -C apps OBJECTS=cat/cat.o OUTPUT=cat/cat.exe

catsys:
	make -C apps OBJECTS=catsys/catsys.o OUTPUT=catsys/catsys.exe

lsdev:
	make -C apps OBJECTS=lsdev/lsdev.o OUTPUT=lsdev/lsdev.exe

umtest:
	make -C apps OBJECTS=umtest/umtest.o OUTPUT=umtest/umtest.exe

create:
	make -C apps OBJECTS=create/create.o OUTPUT=create/create.exe
	
write:
	make -C apps OBJECTS=write/write.o OUTPUT=write/write.exe

#Tools used during the build process
iltools:
	gcc -o tools/bin2c.exe tools/bin2c.c
	gcc -I. -o tools/mkrdisk.exe tools/mkrdisk.c tools/huffman.c

iltoolsclean:
	rm -f tools/bin2c.exe tools/mkrdisk.exe

run:
	echo "" > serlog.txt
	$(EMUL) $(EMUL_ARGS)

clean:
	rm -rf $(ARCH_INCLUDE_COMMON)
	rm -f $(OBJECTS) $(OUTPUT) $(ISO) $(ISOFILES)/boot/$(OUTPUT) $(MISC_CRAP)
	rm -f $(RDISK_OUTPUT) $(ISOFILES)/boot/$(RDISK_OUTPUT)
	make -C apimod clean
	make -C apps OBJECTS="ishell/ishell.o ishell/parse.o" OUTPUT="ishell/ishell.exe" clean
	make -C apps OBJECTS=testapp/testapp.o OUTPUT=testapp/testapp.exe clean
	make -C apps OBJECTS=ls/ls.o OUTPUT=ls/ls.exe clean
	make -C apps OBJECTS=cat/cat.o OUTPUT=cat/cat.exe clean
	make -C apps OBJECTS=catsys/catsys.o OUTPUT=catsys/catsys.exe clean
	make -C apps OBJECTS=lsdev/lsdev.o OUTPUT=lsdev/lsdev.exe clean
	make -C apps OBJECTS=create/create.o OUTPUT=create/create.exe clean
	make -C apps OBJECTS=write/write.o OUTPUT=write/write.exe clean
	make -C apps OBJECTS=umtest/umtest.o OUTPUT=umtest/umtest.exe clean

%.o: %.c kernel/config.h Makefile
	$(CC) $(CFLAGS) $< -o $@

%.o: %.s kernel/config.h Makefile
	$(AS) $(ASFLAGS) $< -o $@
